<?php
class UserModel extends Model {
    public function __construct() {
        parent::__construct();
        $this->tables = array(DB_FREFIX.'user');
        $this->fields = array('id','user','pass','date');
        $this->check = new UserCheck();
        list(
                $this->R['id'],
                $this->R['user'],
                $this->R['pass']
        ) = $this->getRequest()->getParams(
            array(
                isset($_GET['id']) ? $_GET['id'] : null,
                isset($_POST['user']) ? $_POST['user'] : null,
                isset($_POST['pass']) ? sha1($_POST['pass']) : null
            )
        );
    }

    public function reg() {
        $condition = array('user='.$this->R['user']);
        if(!$this->check->regCheck($this,$condition)) $this->check->displayError();
        $needData = $this->getRequest()->filter($this->fields);
        $needData['pass'] = sha1($needData['pass']);
        $needData['date'] = Tool::getDate();
        return parent::add($needData);
    }

    public function login() {
        $condition = array('user='.$this->R['user'].' AND pass='.$this->R['pass']);
        if(!$this->check->loginCheck($this,$condition)) $this->check->displayError();
        if(isset($_POST['keep']) && !Validate::isNullString($_POST['keep'])){
            setcookie('user',$_POST['user'],time() + 60 * 60 * 24 * 7);
        } else {
            setcookie('user',$_POST['user']);
        }
        return true;
    }

    public function logout() {
        setcookie('user','');
        return true;
    }

    public function checkUserName() {
        $this->check->checkUserName($this,array('user='.$this->R['user']));
    }

}


?>